<?php
namespace Todo\Model\Sys;

/**
 * Class TenantModel 租户 模型
 * @package Todo\Model\Sys
 */
class TenantModel extends SysBaseModel
{
    protected $_validate = array(
        array("code", "require", "代码必须！"),
        array("code", "2,10", "代码长度超出限制！", 3, "length"),
        array("title", "require", "名称必须！"),
        array("title", "2,50", "名称长度超出限制！", 3, "length"),
        array("domain", "3,20", "独立域名长度超出限制！", 2, "length"),
        array("add_time", "require", "开通时间必须！"),
        array("expired_time", "require", "过期时间必须！"),
        array("status", "require", "状态必须！")
    );

    protected $_auto = array(
        array("update_time", 'auto_datetime', self::MODEL_BOTH, "function",'now'),
        array("add_time", 'auto_date', self::MODEL_BOTH, "function",'now'),
        array("expired_time", 'auto_date', self::MODEL_BOTH, "function",'now'),
        array("is_del", "0")
    );

    public function format(&$info, $fields = array('status'))
    {
        if(!$fields) return;

        if(!is_array($fields)) $fields = explode(',', $fields);
        foreach($fields as $field){
            if($field == 'org' && $info['org_id']){
                $info['org_name'] = D('Todo/Sys/Organization')->keyFind($info['org_id'], 'title');
            }
        }
    }
}